package org.example.schedule;

import lombok.extern.slf4j.Slf4j;
import org.example.enumcode.StatusEnum;
import org.example.mapper.ScheduleTaskMapper;
import org.example.scheduledEntity.ScheduSpringCron;
import org.example.util.SpringUtils;
import org.springframework.stereotype.Component;


/**
 * @author 张在成
 */
@Component
public interface ScheduleTask extends Runnable {


    public static final   int i = 0;

    /**
     * springboot自带的定时任务执行器
     */
    //@Scheduled(cron = "*/5 * * * * ?")
    /*public void printLog() {
        log.info("每隔5s触发一次钉钉报警:{},对其数量进行++{}",Thread.currentThread().getId(),++i);
    }*/


    void execute();

    /**
     * 实现定时任务启用或者禁用功能
     */
    @Override
    default void run() {

        ScheduleTaskMapper  taskMapper= SpringUtils.getBean(ScheduleTaskMapper.class);

        ScheduSpringCron springCron = taskMapper.getScheduleTaskByCronKey(this.getClass().getName());

        //禁用定时任务
        if (StatusEnum.DISABLED.getCode().equals(springCron.getStatus())){
            return;
        }
        execute();
    }
}
